package com.course.business.controller.admin;

import com.course.server.common.Result;
import com.course.server.dto.SectionDto;
import com.course.server.query.SectionPageQuery;
import com.course.server.service.SectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author: Durian
 * @date: 2021/5/1 20:53
 */
@Validated
@RestController
@RequestMapping("admin/sections")
public class SectionController {

    /**
     * 用于日志记录使用
     */
    public static final String BUSINESS_NAME = "小节";

    @Resource
    private SectionService sectionService;

    @GetMapping
    public Result list(@Validated SectionPageQuery request) {
        return Result.success(sectionService.list(request.getPage(), request.getSize(), request.getChapterId(), request.getCourseId()));
    }

    @PostMapping
    public Result save(@RequestBody @Validated SectionDto sectionDto) {
        sectionService.save(sectionDto);
        return Result.success();
    }

    @DeleteMapping("{sectionId}")
    public Result delete(@PathVariable("sectionId") @NotBlank(message = "待删除小节ID不能为空") String sectionId) {
        sectionService.delete(sectionId);
        return Result.success();
    }
}
